package cn.koala.key.scraped.service.impl;

import cn.koala.key.api.KeyManagementApi;
import cn.koala.key.common.feign.FeignUtils;
import cn.koala.key.common.utils.OrderNoGenerator;
import cn.koala.key.scraped.domain.dto.ScrapedOrderDTO;
import cn.koala.key.scraped.domain.dto.ScrapedSerachDTO;
import cn.koala.key.scraped.domain.vo.ScrapedKeyInfoExcel;
import cn.koala.key.scraped.domain.vo.ScrapedOrderDetailVO;
import cn.koala.key.scraped.domain.vo.ScrapedOrderVO;
import cn.koala.key.scraped.service.ScrapedOrderService;
import cn.koala.key.stock.domain.vo.KeyInfoVO;
import cn.seed.common.core.Result;
import cn.seed.common.core.ResultList;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 
 * @author shuai.xu
 * @date 2019/05/27
 **/
@Service
public class ScrapedOrderServiceImpl implements ScrapedOrderService {

    @Override
    public Result<ResultList<ScrapedOrderVO>> scrapedList(ScrapedSerachDTO scrapedSerachDTO) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).scrapedList(scrapedSerachDTO);
    }

    @Override
    public Result<ScrapedOrderDetailVO> getScrapedDetailByOrderNo(String orderNo) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).getScrapedDetailByOrderNo(orderNo);
    }

    @Override
    public List<ScrapedKeyInfoExcel> dataProcessing(List<KeyInfoVO> data) {
        List<ScrapedKeyInfoExcel> scrapedKeyInfoExcelList = Lists.newArrayList();
        data.forEach(keyInfoVO -> {
            ScrapedKeyInfoExcel scrapedKeyInfoExcel = new ScrapedKeyInfoExcel();
            scrapedKeyInfoExcel.setCarNo(keyInfoVO.getCarNo());
            scrapedKeyInfoExcel.setKeyBarCode(keyInfoVO.getKeyBarCode());
            scrapedKeyInfoExcel.setCarVin(keyInfoVO.getCarVin());
            scrapedKeyInfoExcel.setWarehouseLocation(keyInfoVO.getWarehouseLocation());
            scrapedKeyInfoExcelList.add(scrapedKeyInfoExcel);
        });
        return scrapedKeyInfoExcelList;
    }

    @Override
    public Result insertOrUpdate(ScrapedOrderDTO scrapedOrderDTO) {
        String orderNo = null;
        if (scrapedOrderDTO.getScrapedOrder().getId() == null){
            orderNo = OrderNoGenerator.generateOrderNo(0000,OrderNoGenerator.ORDER_TYPE_SCRAP);
            scrapedOrderDTO.getScrapedOrder().setOrderNo(orderNo);
        }else{
            orderNo = scrapedOrderDTO.getScrapedOrder().getOrderNo();
        }
        scrapedOrderDTO.getScrapedOrder().setAmount(scrapedOrderDTO.getScrapedOrderDetailList().size());
        String finalOrderNo = orderNo;
        scrapedOrderDTO.getScrapedOrderDetailList().forEach(scrapedOrderDetail ->
            scrapedOrderDetail.setOrderNo(finalOrderNo));
        return FeignUtils.getHttpApi(KeyManagementApi.class).insertOrUpdateScrapedOrder(scrapedOrderDTO);
    }

    @Override
    public Result invalidOrSubmit(String orderNo, Integer status) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).invalidOrSubmitScraped(orderNo,status);
    }
}
